﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eloamComLib;
using BLL;
using Model;

namespace ZJUploadTool
{
    public partial class frmImageSC : Form
    {
        /// <summary>
        /// 图片类型，安检报告单，合格标志，行驶证照片
        /// </summary>
        public string strImageType { get; set; }
        
        public string JCLSH { get; set; }

        public FrmUpload frmUplad { get; set; }

        private EloamGlobal global;
        private EloamVideo m_pVideo;

        //设备列表
        private List<EloamDevice> m_deviceList;
        public frmImageSC()
        {
            InitializeComponent();
            global = new EloamGlobal();
            m_deviceList = new List<EloamDevice>();
            FormInit();
        }

        public void FormInit()
        {
            //传入设备状态改变事件
            global.DevChange += DevChangeEventHandler;

            //初始化设备
            global.InitDevs();
        }

        //设备状态改变事件响应
        public void DevChangeEventHandler(int type, int idx, int dbt)
        {
            if (1 == type)
            {
                if (1 == dbt)//设备到达
                {
                    EloamDevice tempDevice = (EloamDevice)global.CreateDevice(1, idx);
                    m_deviceList.Add(tempDevice);

                    selectDevice.Items.Add(tempDevice.GetFriendlyName());
                    if (-1 == selectDevice.SelectedIndex)
                    {
                        selectDevice.SelectedIndex = 0;//改变所选设备
                    }

                }
                else if (2 == dbt)//设备丢失
                {
                    EloamDevice tempDevice = m_deviceList[idx];

                    m_deviceList[idx].Destroy();
                    m_deviceList.RemoveAt(idx);
                    selectDevice.Items.RemoveAt(idx);
                    if (-1 == selectDevice.SelectedIndex)
                    {
                        if (0 != m_deviceList.Count)
                        {
                            selectDevice.SelectedIndex = 0;
                        }
                        else
                        {
                            selectDevice.Items.Clear();
                        }
                    }
                }
            }
        }

        private void selectDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = selectDevice.SelectedIndex;//记录当前所选设备
            selectMode.Items.Clear();

            if (-1 != idx)
            {
                EloamDevice tempDevice = m_deviceList[idx];

                //加载模式列表
                int subtype = tempDevice.GetSubtype();
                if (0 != (subtype & 1))
                {
                    selectMode.Items.Add("YUY2");
                }
                if (0 != (subtype & 2))
                {
                    selectMode.Items.Add("MJPG");
                }
                if (0 != (subtype & 4))
                {
                    selectMode.Items.Add("UYVY");
                }

                //若为辅摄像头，优先选择MJPG方式
                if (1 != tempDevice.GetEloamType() && (0 != (subtype & 2)) && (0 != (subtype & 1)))
                {
                    selectMode.SelectedIndex = 1;
                }
                else
                {
                    selectMode.SelectedIndex = 0;
                }

                openVideo.Enabled = true;
            }
            else
            {
                openVideo.Enabled = false;
            }
        }

        private void selectMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = selectDevice.SelectedIndex;//记录当前所选设备

            selectResolution.Items.Clear();

            if (-1 != idx)
            {
                EloamDevice tempDevice = m_deviceList[idx];
                //加载分辨率列表
                int subtype = -1;
                string curModeString = selectMode.SelectedItem.ToString();
                subtype = (curModeString == "YUY2" ? 1 :
                            (curModeString == "MJPG" ? 2 :
                                (curModeString == "UYVY" ? 4 :
                                    -1)));
                if (-1 != subtype)
                {
                    int count = tempDevice.GetResolutionCountEx(subtype);
                    for (int i = 0; i < count; ++i)
                    {
                        int width = tempDevice.GetResolutionWidthEx(subtype, i);
                        int height = tempDevice.GetResolutionHeightEx(subtype, i);

                        string str = width.ToString() + "*" + height.ToString();
                        selectResolution.Items.Add(str);
                    }
                    selectResolution.SelectedIndex = 0;
                }
            }
        }

        private void openVideo_Click(object sender, EventArgs e)
        {
            int devIdx = selectDevice.SelectedIndex;
            string curModeString = selectMode.SelectedItem.ToString();
            int modeIdx = (curModeString == "YUY2" ? 1 :
                            (curModeString == "MJPG" ? 2 :
                                (curModeString == "UYVY" ? 4 :
                                    -1)));
            int resIdx = selectResolution.SelectedIndex;

            if (-1 != devIdx)
            {
                if (null != m_pVideo)
                {
                    m_pVideo.Destroy();
                    m_pVideo = null;
                }

                EloamDevice tempDevice = m_deviceList[devIdx];
                m_pVideo = (EloamVideo)tempDevice.CreateVideo(resIdx, modeIdx);

                if (null != m_pVideo)
                {
                    eloamView.SelectVideo(m_pVideo);
                    eloamView.SetText("打开视频中，请等待...", RGB(255, 255, 255));

                    selectDevice.Enabled = false;
                    selectResolution.Enabled = false;
                    selectMode.Enabled = false;

                    openVideo.Enabled = false;
                    closeVideo.Enabled = true;
                }
            }
        }

        public uint RGB(uint r, uint g, uint b)
        {
            return (((b << 16) | (g << 8)) | r);
        }

        private void shoot_Click(object sender, EventArgs e)
        {
            if (null == m_pVideo)
            {
                return;
            }

            EloamView tempView = (EloamView)eloamView.GetView();
            EloamImage tempImage = (EloamImage)m_pVideo.CreateImage(0, tempView);

            if (null != tempImage)
            {
                RESULT_IMAGE_ZJ_BLL M_RESULT_IMAGE_ZJ_BLL = new RESULT_IMAGE_ZJ_BLL();
                var vImages = M_RESULT_IMAGE_ZJ_BLL.GetModelList("JCLSH='" + JCLSH + "'");
                if (vImages.Length > 0)
                {
                    string strImages = tempImage.GetBase64(2, 0x0800);
                    int len = strImages.Length;
                    if (strImageType == "AJBGD")
                    {
                        vImages[0].AJBGD = strImages;
                    }
                    if (strImageType == "AJHGBZ")
                    {
                        vImages[0].AJHGBZ = strImages;
                    }
                    if (strImageType == "JDCXSZ")
                    {
                        vImages[0].JDCXSZ = strImages;
                    }
                    if (strImageType == "ZJBGD")
                    {
                        vImages[0].ZJBGD = strImages;
                    }
                    if (strImageType == "ZJYQ")
                    {
                        vImages[0].ZJYQ = strImages;
                    }

                    bool bSuc = M_RESULT_IMAGE_ZJ_BLL.Update(vImages[0], vImages[0].ID);
                    if (bSuc)
                    {
                        MessageBox.Show("拍照成功！");
                        frmUplad.GetAllImages();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("拍照失败！");
                        return;
                    }
                }
                else
                {
                    RESULT_IMAGE_ZJ M_RESULT_IMAGE_ZJ = new RESULT_IMAGE_ZJ();
                    M_RESULT_IMAGE_ZJ.JCLSH = JCLSH;
                    string strImages = tempImage.GetBase64(2, 0x0800);
                    int len = strImages.Length;
                    if (strImageType == "AJBGD")
                    {
                        M_RESULT_IMAGE_ZJ.AJBGD = strImages;
                    }
                    if (strImageType == "AJHGBZ")
                    {
                        M_RESULT_IMAGE_ZJ.AJHGBZ = strImages;
                    }
                    if (strImageType == "JDCXSZ")
                    {
                        M_RESULT_IMAGE_ZJ.JDCXSZ = strImages;
                    }
                    if (strImageType == "ZJBGD")
                    {
                        M_RESULT_IMAGE_ZJ.ZJBGD = strImages;
                    }
                    if (strImageType == "ZJYQ")
                    {
                        M_RESULT_IMAGE_ZJ.ZJYQ = strImages;
                    }
                    bool bSuc = M_RESULT_IMAGE_ZJ_BLL.Insert(M_RESULT_IMAGE_ZJ);
                    if (bSuc)
                    {
                        MessageBox.Show("拍照成功！");
                        frmUplad.GetAllImages();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("拍照失败！");
                        return;
                    }
                }

            }
        }

        private void frmImageSC_FormClosing(object sender, FormClosingEventArgs e)
        {
            closeVideo_Click(new object(), new EventArgs());

            int count = m_deviceList.Count;
            if (count != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    m_deviceList[i].Destroy();
                }
            }
            m_deviceList.Clear();

            global.DeinitBarcode();

            global.DeinitBiokey();
            global.DeinitIdCard();
            global.DeinitDevs();
            global.DeinitOcr();

            //传出设备状态改变事件
            global.DevChange -= DevChangeEventHandler;
        }

        private void closeVideo_Click(object sender, EventArgs e)
        {
            if (null != m_pVideo)
            {
                eloamView.SetText(null, 0);
                m_pVideo.Destroy();
                m_pVideo = null;
            }

                eloamView.SetText(null, 0);
        }
    }
}
